package com.invengo.testrfid.mvp.presenter;

import android.app.Application;

import com.invengo.testrfid.app.entity.MessageEntity;
import com.invengo.testrfid.app.proxy.ProxyNetWorker;
import com.invengo.testrfid.app.proxy.ProxyReadTag;
import com.invengo.testrfid.app.utils.Constant;
import com.invengo.testrfid.app.utils.GsonUtils;
import com.invengo.testrfid.app.utils.MyLogUtils;
import com.invengo.testrfid.mvp.contract.SearchContract;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;


@ActivityScope
public class SearchCagePresenter extends BasePresenter<SearchContract.Model, SearchContract.View> {

    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public SearchCagePresenter(SearchContract.Model model, SearchContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void connectReader() {

        ProxyReadTag.getInstance().connectReader(new ProxyReadTag.ConnectReaderStatue() {
            @Override
            public void onConnectSuccess() {

                mRootView.showMessage("connectSuccess");
            }

            @Override
            public void onConnectFailed() {

                mRootView.showMessage("connectFailed");
            }
        });
    }

    public void readTagTid() {

        ProxyReadTag.getInstance().readTagTid(new ProxyReadTag.ReadTagCallback() {

            @Override
            public void onReadSuccess(String tid) {

                if (mRootView != null)
                    mRootView.initTid(tid);
            }

            @Override
            public void onFailed(String message) {

                if (mRootView != null)
                    mRootView.showMessage(message);
            }
        });
    }

    /**
     * 根据id来查询绑定关系
     *
     * @param tid
     */
    public void queryBindByTid(String tid) {

        if(tid.trim().length()== 0){
            mRootView.showMessage("查询数据不能为空");
            return;
        }

        mRootView.showLoading();
        String url = "http://" + Constant.byIp + "/api/CageInfo/CageQuery?tid=" + tid;
        ProxyNetWorker.getInstance().getServerData(url, new ProxyNetWorker.RequestCallback() {
            @Override
            public void onSuccess(String data) {

                mRootView.hideLoading();
                mRootView.showMessage(data);
                MyLogUtils.log("test", data, 4);
                MessageEntity messageEntity = (MessageEntity) GsonUtils.jsonToBean(data, MessageEntity.class);

                if(messageEntity == null){
                    mRootView.showMessage("该笼车未初始化！");
                    return;
                }
                if (messageEntity.getStatus().equals("1")) {
                    mRootView.initCageNo(messageEntity.getInfo());
                }else {

                    mRootView.showMessage(messageEntity.getInfo());
                }
            }

            @Override
            public void onFailed(String msg) {

                mRootView.hideLoading();
                mRootView.showMessage(msg);
                MyLogUtils.log("test", "failed query" + msg, 4);
            }
        });
    }

    public void stopReadTag() {

        ProxyReadTag.getInstance().stopRead(new ProxyReadTag.StopReadCallback() {

            @Override
            public void onStopSuccess() {

            }

            @Override
            public void onStopFailed() {

                mRootView.showMessage("停止读取标签操作失败，请重试");
            }
        });
    }
}
